<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<meta name="decorator" content="sysDefault" content="text/html; charset=utf-8" />
<script src="${staticPath}/js/pospay/opendialog.js" type="text/javascript"></script>

<script type="text/javascript">
	//数据字典信息
	var sysDict = "";
	//数据字典明细信息
	var sysDictDetailList = "";
	//数据字典明细被删ID
	var delSysDictDetailList = "";
	$(document).ready(function() {
			$("#key").focus();
			$("#dataForm").validate({
				submitHandler: function(form){
					var dictType = $.trim(form.dictType.value);
					var id = $.trim(form.id.value);
					$.get("${contextPath}/sys/sysdict/validate?dictType="+dictType+"&notId="+id,function(result){
						if(result == 0){
							var divArr = $("#detailList").children();
							//验证明细信息
							var result = validateDetail(divArr);
							if(result == false){
								alert("对不起,请填写数据字典明细的具体内容!");
							}else{
								//数据字典信息拼装json
								sysDict += "{'id':'"+id+"',";
								sysDict += "'name':'"+form.name.value+"',";
								sysDict += "'dictType':'"+dictType+"',";
								sysDict += "'desc':'"+form.desc.value+"',";
								sysDict += "'bup1':'"+form.bup1.value+"',";
								sysDict += "'bup2':'"+form.bup2.value+"',";
								sysDict += "'bup3':'"+form.bup3.value+"'}";
								
								//数据字典明细信息拼装json
								sysDictDetailList += "[";
								for(var i = 0; i < divArr.length; i++){
									sysDictDetailList += "{";
									var inputArr = divArr[i].getElementsByTagName("input");
									for(var j = 0; j < inputArr.length; j++){
										if(inputArr[j].name == "did"){
											sysDictDetailList += "'id':'"+inputArr[j].value+"'";
										}else{
											sysDictDetailList += "'"+inputArr[j].name+"':'"+inputArr[j].value+"'";
										}
										if(j < inputArr.length-1){
											sysDictDetailList += ",";
										}else{
											sysDictDetailList += "}";
										}
									}
									if(i < divArr.length-1){
										sysDictDetailList += ",";
									}
								}
								sysDictDetailList += "]";
								
								//数据字典明细被删ID拼装json
								if(delSysDictDetailList != ""){
									delSysDictDetailList = "["+delSysDictDetailList.substring(0,delSysDictDetailList.lastIndexOf(","))+"]";
								}
								
								var param = {"sysDict":sysDict,"sysDictDetailList":sysDictDetailList,"delSysDictDetailList":delSysDictDetailList};
								$.post(form.action,param,function(data){
									Oosp_Loading('正在提交，请稍等...');
									window.open('${contextPath}'+data,"_self");
								});
							}
						}else{
							alert("对不起,类型已经存在,请重新填写!");
							$("#dictType").focus();
						}
					});
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
		});
	});
	
	function addDetail(){
		var detailHtml = '<div class="form-group" >'+
							'<input name="did" value="0" type="hidden"  />'+
							'<label for="saletype" class="col-sm-1 control-label">KEY:</label>'+
							'<div class="col-sm-2">'+
								'<input name="key" maxlength="20" style="width: 150px" class="form-control" />'+
							'</div>'+
							'<label for="saletype" class="col-sm-1 control-label">显示文本:</label>'+
							'<div class="col-sm-2">'+
								'<input name="value" maxlength="20" style="width: 150px" class="form-control" />'+
							'</div>'+
							'<label for="saletype" class="col-sm-1 control-label">排序:</label>'+
							'<div class="col-sm-2">'+
								'<input name="sort" maxlength="20" style="width: 150px" class="form-control" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\\D/g,\'\')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\\D/g,\'\')}" onchange="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\\D/g,\'\')}"/>'+
							'</div>'+
							'<div class="col-sm-3">'+
								'<a href="#" onclick="return delDetail(this);" class="btn btn-danger" style="width: 100px;color:white;" >删除</a>'+
							'</div>'+
						 '</div>';
		$("#detailList").append(detailHtml);
	}
	
	//删除明细
	function delDetail(obj){
		var $this = $(obj);
		var formGroup = $this.parents(".form-group");
		var inputArr = formGroup.find("input");
		for(var i = 0; i < inputArr.length; i++){
			if(inputArr[i].name == "did"){
				var tempId = inputArr[i].value;
				if(tempId == 0){
					formGroup.remove();
				}else{
					var flag = confirm("是否删除明细数据","确认删除？");
					if (flag) {
						delSysDictDetailList += tempId+",";
						formGroup.remove();
					}else{
						return false;
					}
				}
			}
		}
	}
	
	function validateDetail(divArr){
		for(var i = 0; i < divArr.length; i++){
			var inputArr = divArr[i].getElementsByTagName("input");
			for(var j = 0; j < inputArr.length; j++){
				if(inputArr[j].name == "key" || inputArr[j].name == "value" || inputArr[j].name == "sort"){
					var value = $.trim(inputArr[j].value);
					if(value == "" || value == null || typeof(value)=="undefined"){
						inputArr[j].focus();
						return false;
					}
				}
			}
		}
		return true;
	}
	
</script>
</head>
<body>
	<div class="container-fluid">
		<div class="row">
			<div class="col-xs-12">
				<form:form id="dataForm" modelAttribute="sysDict"
					action="${contextPath}/sys/sysdict/save" method="post"
					class="form-horizontal" role="form">
					
					<div class="panel panel-default">
						<div class="panel-heading">
							<h3 class="panel-title">
								新增数据 <a class="btn btn-default btn-sm pull-right" role="button"
									href="${contextPath}/sys/sysdict/list" >返回</a>
							</h3>
						</div>
						<div class="panel-body">
							<form:hidden path="id" />
							<tags:message content="${message}" />
							<div class="form-group">
								<label for="saleNo" class="col-sm-2 control-label">名称:</label>
								<div class="col-sm-2">
									<form:input id="name" path="name" maxlength="20"
										class="form-control required" />
								</div>
							</div>
							<div class="form-group">
								<label for="salenoOffline" class="col-sm-2 control-label">类型:</label>
								<div class="col-sm-2">
									<form:input id="dictType" path="dictType" maxlength="20"
										class="form-control required" />
								</div>
							</div>
							<div class="form-group">
								<label for="saleMac" class="col-sm-2 control-label">描述:</label>
								<div class="col-sm-2">
									<form:textarea id="desc" path="desc" class="form-control"/>
								</div>
							</div>
							<div class="form-group">
								<label for="saleChannel" class="col-sm-2 control-label">备用1:</label>
								<div class="col-sm-2">
									<form:input id="bup1" path="bup1" maxlength="20"
										class="form-control" />
								</div>
							</div>
							<div class="form-group">
								<label for="saletype" class="col-sm-2 control-label">备用2:</label>
								<div class="col-sm-2">
									<form:input id="bup2" path="bup2" maxlength="20"
										class="form-control" />
								</div>
							</div>
							<div class="form-group">
								<label for="saletype" class="col-sm-2 control-label">备用3:</label>
								<div class="col-sm-2">
									<form:input id="bup3" path="bup3" maxlength="20"
										class="form-control" />
								</div>
							</div>
						</div>
						
						<div class="panel-heading">
							<h3 class="panel-title">
								新增明细数据 <a class="btn btn-success btn-sm pull-right" role="button"
									href="javascript:void(0)" onclick="addDetail();" style="color:white;">新增一条</a>
							</h3>
						</div>
						<div class="panel-body" id="detailList">
							<c:if test="${list != null}">
								<c:forEach items="${list}" var="sysDictDetail">
									<div class="form-group" >
										<input name="did" value="${sysDictDetail.id}" type="hidden"  />
										<label for="saletype" class="col-sm-1 control-label">KEY:</label>
										<div class="col-sm-2">
											<input name="key" value="${sysDictDetail.key}" maxlength="20" style="width: 150px" class="form-control" />
										</div>
										<label for="saletype" class="col-sm-1 control-label">显示文本:</label>
										<div class="col-sm-2">
											<input name="value" value="${sysDictDetail.value}" maxlength="20" style="width: 150px" class="form-control" />
										</div>
										<label for="saletype" class="col-sm-1 control-label">排序:</label>
										<div class="col-sm-2">
											<input name="sort" value="${sysDictDetail.sort}" maxlength="20" style="width: 150px" class="form-control" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onchange="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" />
										</div>
										<div class="col-sm-3">
											<a href="#" onclick="return delDetail(this);" class="btn btn-danger" style="width: 100px;color:white;" >删除</a>
										</div>
									</div>
								</c:forEach>
							</c:if>
						</div>
						
						<div class="form-group">
							<div class="col-sm-offset-2 col-sm-10">
								<input class="btn btn-primary" type="submit" value="保 存" /> 
								<a class="btn btn-default" href="${contextPath}/sys/sysdict/list" >返 回</a>
							</div>
						</div>
					</div>
					
				</form:form>
			</div>
		</div>
	</div>
</body>
</html>